package com.android.functionmanagement.ui;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Switch;
import android.widget.TextView;

import com.android.functionmanagement.BaseActivity;
import com.android.functionmanagement.util.SharedPreferencesUtils;
import com.uwatch.helowin.R;

public class AlertManometryActivity extends BaseActivity implements
		OnCheckedChangeListener {

	private TextView mMaTimeTx;
	private TextView mMaDaysTx;
	private TextView mMaPeriodTx;
	private Switch mSwitchRing;
	private SharedPreferences mSh;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alert_manometry_layout);
		mMaTimeTx = (TextView) findViewById(R.id.ma_time_tx);
		mMaDaysTx = (TextView) findViewById(R.id.ma_days_tx);
		mMaPeriodTx = (TextView) findViewById(R.id.ma_period_tx);
		mSwitchRing = (Switch) findViewById(R.id.ma_sw_ring);
		mSwitchRing.setOnCheckedChangeListener(this);
		mSh = PreferenceManager.getDefaultSharedPreferences(this);
		mSwitchRing.setChecked(mSh.getBoolean(
				AlertManometrySettings.MA_RING_ONOFF, getResources()
						.getBoolean(R.bool.def_enable_mo_ring)));
		updateDisplayData();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.android.functionmanagement.BasePreferenceActivity#updateDisplayData()
	 */
	@Override
	protected void updateDisplayData() {
		// TODO Auto-generated method stub
		super.updateDisplayData();
		String[] archives = SharedPreferencesUtils
				.getParam(this, "archive", "").toString().replace("{", "")
				.replace("}", "").split(",");
		String maTime = mMaTimeTx.getText().toString();
		String maDays = null;
		String maPeriod = null;
		for (String archive : archives) {
			if (archive.contains("hours=")) {
				maTime = archive.substring("hours=".length()
						+ archive.indexOf("hours="));
				continue;
			} else if (archive.contains("days=")) {
				maDays = archive.substring(
						"days=".length() + archive.indexOf("days="))
						.replaceAll("\\D+", ",");
				continue;
			} else if (archive.contains("period=")) {
				maPeriod = archive.substring(
						"period=".length() + archive.indexOf("period="))
						.replaceAll("\\D+", ",");
				if ("0".equals(maPeriod))
					maPeriod = getString(R.string.every_day);
				else if ("1".equals(maPeriod))
					maPeriod = getString(R.string.every_week);
				continue;
			}
		}
		mMaTimeTx.setText(maTime);
		mMaDaysTx.setText(maDays);
		mMaPeriodTx.setText(maPeriod);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.widget.CompoundButton.OnCheckedChangeListener#onCheckedChanged
	 * (android.widget.CompoundButton, boolean)
	 */
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		switch (buttonView.getId()) {
		case R.id.ma_sw_ring:
			mSh.edit()
					.putBoolean(AlertManometrySettings.MA_RING_ONOFF, isChecked)
					.commit();
			break;
		default:
			break;
		}
	}
}
